package com.cn.jdia.dc.utils.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DBManager4Mysql {

	public static final String DRIVER = "com.mysql.jdbc.Driver";

	private static DBManager4Mysql per = null;
	private Connection conn = null;
	private Statement stmt = null;

	private DBManager4Mysql() {

	}

	public static DBManager4Mysql createInstance() {
		if (per == null) {
			per = new DBManager4Mysql();
			per.initDB();
		}
		return per;
	}

	public void initDB() {
		try {
			Class.forName(DRIVER);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int connectDB(String url, String user, String pass) {
		try {
			conn = DriverManager.getConnection(url, user, pass);
			stmt = conn.createStatement();
		} catch (SQLException e) {
			//e.printStackTrace();
			return -1;
		}
		return 0;
	}

	public void closeDB() {
		try {
			if(stmt!=null){
				stmt.close();
			}
			if(conn!=null){
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
